package com.zhongyu.item.consumer.service.Impl;

import com.zhongyu.comm.pojo.Cinema;
import com.zhongyu.comm.pojo.Image;
import com.zhongyu.comm.pojo.Item;
import com.zhongyu.comm.pojo.ItemType;
import com.zhongyu.item.consumer.client.CinemaClient;
import com.zhongyu.item.consumer.client.ImageClient;
import com.zhongyu.item.consumer.client.ItemClient;
import com.zhongyu.item.consumer.client.ItemTypeClient;
import com.zhongyu.item.consumer.service.IndexService;
import com.zhongyu.item.consumer.service.ItemService;
import com.zhongyu.item.consumer.vo.FloorItemsVo;
import com.zhongyu.item.consumer.vo.FloorItemVo;
import com.zhongyu.item.consumer.vo.ItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("indexService")
@Slf4j
public class IndexServiceImpl implements IndexService {
   /* @Resource
    private ItemClient itemClient;
    @Resource
    private ImageClient imageClient;
    @Resource
    private CinemaClient cinemaClient;*/

    @Resource
    private ItemTypeClient itemTypeClient;

    @Resource
    private ItemService itemService;


    @Override
    public List<FloorItemsVo> searchFloorItems() {
        List<ItemType> itemTypes = itemTypeClient.findByKey();
        return itemTypes.stream().map(itemType -> {
            FloorItemsVo floorItemsVo = new FloorItemsVo();
            floorItemsVo.setFloor(itemType.getKey());
            floorItemsVo.setItemTypeId(itemType.getId());
            floorItemsVo.setItemTypeName(itemType.getItemType());
        /* List<Item> list= itemClient.findTopN(itemType.getId(),5);
             for (Item item:list){
                 FloorItemVo itemVo=new FloorItemVo();
                 BeanUtils.copyProperties(item,itemVo);
                 itemVo.setStartDate(item.getStartTime());
                 itemVo.setEndDate(item.getEndTime());
                 Image image= imageClient.findOne(item.getId(),2,1);
                 itemVo.setImgUrl(image.getImgUrl());
         Cinema cinema= cinemaClient.findById(item.getCinemaId());
           itemVo.setAddress(cinema.getAddress());
           itemVo.setAreaId(cinema.getAreaId());
           itemVo.setAreaName(cinema.getAreaName());
             floorItemsVo.getItems().add(itemVo);
         }*/
            List<ItemVo> list = itemService.searchItems();
            List<FloorItemVo> floorItemsVoList = list.stream().filter(itemVo -> itemVo.getItemType1Id().equals(floorItemsVo.getItemTypeId()))
                    .map(itemVo -> {
                        //log.debug("测试itemVo:"+itemVo.toString());
                        FloorItemVo floorItemVo = new FloorItemVo();
                        BeanUtils.copyProperties(itemVo, floorItemVo);
                        //  log.debug("测试floorItem"+floorItemVo.getImgUrl());
                        return floorItemVo;
                    })
                    .collect(Collectors.toList());
            floorItemsVo.setItems(floorItemsVoList);
            return floorItemsVo;

        }).collect(Collectors.toList());
    }

  /*  @Scheduled(fixedRate = 3000)
    public  void  testTack(){
        log.debug("测试定期执行任务");
    }*/
  /* @Scheduled(cron = "0 0 3 * * ?")
   @Cacheable(value = "items")
    @Override
    public List<ItemVo> searchItems() {
       log.debug("远程查询，并把结果放入redis缓存");
        List<ItemVo> itemVos=new ArrayList<ItemVo>();
        List<ItemType> itemTypes= itemTypeClient.findByKey();
        itemTypes.stream().forEach(itemType -> {
            List<Item> list= itemClient.findTopN(itemType.getId(),5);
            for (Item item:list){
                ItemVo itemVo=new ItemVo();
                BeanUtils.copyProperties(item,itemVo);
                itemVo.setStartDate(item.getStartTime());
                itemVo.setEndDate(item.getEndTime());
                Image image= imageClient.findOne(item.getId(),2,1);
                itemVo.setImgUrl(image.getImgUrl());
                Cinema cinema= cinemaClient.findById(item.getCinemaId());
                itemVo.setAddress(cinema.getAddress());
                itemVo.setAreaId(cinema.getAreaId());
                itemVo.setAreaName(cinema.getAreaName());
                itemVos.add(itemVo);
            }
        });
        return  itemVos;
    }*/
}
